package cn.iocoder.yudao.module.member.service.user;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserDetailPriceUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPriceUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserDetailPriceUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserPriceUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserPriceConvert;
import cn.iocoder.yudao.module.member.convert.user.MemberUserPriceDetailConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserPriceDetailDo;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserPriceDo;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserPriceDetailMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserPriceMapper;
import cn.iocoder.yudao.module.member.dal.redis.no.OrderNoRedisDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Service
@Valid
@Slf4j
public class MemberUserPriceServiceImpl implements MemberUserPriceService{

    @Resource
    private MemberUserPriceMapper memberUserPriceMapper;

    //价格编码
    @Resource
    private OrderNoRedisDAO orderNoRedisDAO;
    //用户私有价格生成
    private final String PRICE_CODE = "private:price:code";
    @Resource
    private MemberUserPriceDetailMapper memberUserDetailPriceMapper;

    /**
     * 通过用户查询单独的价格配置
     *
     * @param userId 手机
     * @return 用户对象
     */
    @Override
    public List<MemberUserPriceDo> getUserByUserId(Long userId) {
        return memberUserPriceMapper.selectByUserId(userId);
    }

    /**
     * 修改单独价格信息
     *
     * @param userId
     * @param prices
     */
    @Override
    public void updateUserPrice(Long userId, List<MemberUserPriceUpdateReqVO> prices) {
        if (CollectionUtil.isEmpty(prices)){
            return;
        }
        memberUserPriceMapper.deleteByUserId(userId);
        memberUserDetailPriceMapper.deleteByUserId(userId);
        List<MemberUserPriceDo> saveList = new ArrayList<>();
        List<MemberUserPriceDetailDo> saveDetailList = new ArrayList<>();
        for (int i = 0; i < prices.size(); i++) {
            MemberUserPriceDo dataDo = MemberUserPriceConvert.INSTANCE.convert(prices.get(i));
            dataDo.setUserId(userId);
            dataDo.setId(null);
            String generate = orderNoRedisDAO.generate(PRICE_CODE);
            dataDo.setPriceCode(generate);
            saveList.add(dataDo);
            if (CollectionUtil.isNotEmpty(prices.get(i).getDetails())){
                for (MemberUserDetailPriceUpdateReqVO detail : prices.get(i).getDetails()) {
                    MemberUserPriceDetailDo memberUserPriceDetailDo = MemberUserPriceDetailConvert.INSTANCE.convert(detail);
                    memberUserPriceDetailDo.setUserId(userId);
                    memberUserPriceDetailDo.setPriceCode(generate);
                    memberUserPriceDetailDo.setId(null);
                    saveDetailList.add(memberUserPriceDetailDo);
                }
            }
        }
        memberUserPriceMapper.saveBatchs(saveList);
        memberUserDetailPriceMapper.saveBatchs(saveDetailList);
    }

}
